package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonAuth;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class KeChengLessonDelController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @Autowired
    YxyLessonsSchoolLessonChapterRepository chapterRepository;

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyLessonsSchoolLessonAuthRepository authRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/lesson/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        YxyLessonsSchoolLesson lesson = lessonRepository.findById(id);
        if (null != lesson) {

            // 删除章节
            List<YxyLessonsSchoolLessonChapter> chapters = chapterRepository.findByLessonId(id);
            chapterRepository.delete(chapters);

            List<YxyLessonsSchoolLessonItem> items = itemRepository.findByLessonId(id);
            // 删除视频
            for (YxyLessonsSchoolLessonItem item : items) {
                // String videoId = item.getVideoId();
                // YxyLessonsVideo video = videoRepository.findById(videoId);
                // if (null != video) {
                // videoRepository.delete(video);
                // }
            }
            // 删除课时
            itemRepository.delete(items);

            // 删除可耻
            lessonRepository.delete(lesson);

            // 删除课程授权
            List<YxyLessonsSchoolLessonAuth> list = authRepository.findByLessonId(id);
            if (list.size() > 0) {
                authRepository.delete(list);
            }
        }
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String id;

        @Override
        public void autoCheck() {

        }
    }
}
